<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>客户管理</title>
    <link href="../../../static/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../../../static/js/jquery.js"></script>
    <script type="text/javascript" src="../../../static/js/jq-paginator.js"></script>
    <script type="text/javascript">
        //查询条件
        window.datas = {
            conditionInfo: {
                pageNum: "1",
                pageSize: "5",
            },
            status:["未审核","审核中","审核通过","审核不通过"],
        }

        function deltipOpen(target) {
            let index=$(target).parents("tr").index();
            let orderInfo=datas.resultInfo.data[index];
            let orderid=datas.resultInfo.data[index].orderid;
            $("#delOrderId").val(orderid);
            $("#deltip").fadeIn(200);
        }

        function deltipClose() {
            $("#deltip").fadeOut(200);
        }

        function examinetipOpen(target) {
            let index=$(target).parents("tr").index();
            let orderInfo=datas.resultInfo.data[index];
            let orderid=datas.resultInfo.data[index].orderid;
            $("#examinetip #orderid").val(orderid);
            $("#examinetip").fadeIn(200);
        }

        function examinetipclose() {
            $("#examinetip").fadeOut(200);
        }
        //获取condition参数
        function getCondition(){
            this.datas.conditionInfo={
                pageNum:1,
                pageSize:5,
                orderid:$(".orderid").val(),
                customName:$(".customName").val(),
                beOrdertime:$(".beOrdertime").val(),
                afOrdertime:$(".afOrdertime").val(),
                beOrdermoney:$(".beOrdermoney").val(),
                afOrdermoney:$(".afOrdermoney").val(),
                status:$(".status option:selected").val(),
                operatorid:JSON.parse(localStorage.getItem("user")).uid
            }
        }
        //获取分页信息
        function getOrdersPage(conditionInfo) {
            $.ajax({
                url: `/marketOrders`,
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf8",
                data: JSON.stringify(conditionInfo),
                success: (data) => {
                    datas.resultInfo = data.data;
                    reder();
                    if(!data.data.totalPage){
                        datas.resultInfo.totalPage=1;
                    }
                    $('.paginList').jqPaginator('option', {
                        totalPages: datas.resultInfo.totalPage,
                    });
                }
            })
        }
        //数据渲染到页面
        function reder() {
            let customInfo = this.datas.resultInfo.data;
            $(".formbody").html("");
            customInfo.forEach((item, index) => {
                $(".formbody").append(` <tr>
                <td>${(datas.resultInfo.pageNum-1)*datas.resultInfo.pageSize+index+1}</td>
                <td>${item.orderid}</td>
                <td>${item.customName}</td>
                <td>${item.telephone}</td>
                <td>${item.ordertime}</td>
                <td>￥${item.ordermoney}</td>
                <td>${item.operatorName}</td>
                <td>${datas.status[item.status-1]}</td>
                <td>${item.checkName}</td>
                <td>${item.chectime}</td>
                <td>
                    <a href="orderView?orderid=${item.orderid}"style="display:${item.status!=1?'':'none'}" class="tablelink">查看详情</a>
                     <a href="/storage/delivery/deliveryView?orderid=${item.orderid}"style="display:${item.status==3&&item.warehouseId!=''?'':'none'}" class="tablelink">出库详情</a>
                    <a href="orderUpdate?orderid=${item.orderid}" style="display:${item.status==1||item.status==4?'':'none'}" class="tablelink">修改</a>
                    <a href="javascript:void(0);" style="display:${item.status==1?'':'none'}" class="tablelink" onclick="deltipOpen(this)">删除</a>
                    <a href="javascript:void(0);" style="display:${item.status==1||item.status==4?'':'none'}" class="tablelink" onclick="examinetipOpen(this)">提交审核</a>
                </td>
            </tr>`)
            });
            $(".blue").eq(0).text(this.datas.resultInfo.totalCount);
            $(".blue").eq(1).text(this.datas.resultInfo.pageNum);
        }
        //点击查询时
        function search() {
            datas.conditionInfo.pageNum = 1;
            getCondition();
            getOrdersPage(datas.conditionInfo);
        }
        //获取部门职位用户三级联查信息
        function getDeptJobUser(){
            $.ajax({
                url: "/commonDeptJobUser",
                type: "get",
                dataType: "json",
                success:(data)=>{
                    window.datas.deptJobUserInfo=data.data;
                }
            })
        }
        //选择部门后
        function deptChange(){
            let deptJobUSerInfo=datas.deptJobUserInfo;
            let job=[];
            let id=$(".dept option:selected").val();
            for (let i = 0;i< deptJobUSerInfo.length; i++) {
                if(deptJobUSerInfo[i].deptId==id){
                    if(id==1){
                        job=deptJobUSerInfo[i].jobs.slice(0,1);
                    }else {
                        job = deptJobUSerInfo[i].jobs;
                    }
                    break;
                }
            }
            datas.jobs=job
            $(".job").html('<option value="">请选择</option>')
            job.forEach((item,index)=>{
                $(".job").append(`<option value="${item.jobId}">${item.jobName}</option>`);
            })
            jobChange();
        }
        //选择职位后
        function jobChange(){
            let jobs=datas.jobs;
            let users=[];
            let id=$(".job option:selected").val();
            for (let i = 0;i< jobs.length; i++) {
                if(jobs[i].jobId==id){
                    users=jobs[i].users;
                    break;
                }
            }
            $("#user").html('<option value="0">请选择</option>')
            users.forEach((item,index)=>{
                $("#user").append(`<option value="${item.uid}">${item.uname}</option>`);
            })
        }
        //提交审核
        function submitCheck(){
            $.ajax({
                url: `/marketOrders/${$("#user").val()}/${$("#orderid").val()}`,
                type: "put",
                dataType: "json",
                contentType: "application/json;charset=utf8",
                data: JSON.stringify(datas.conditionInfo),
                success:(data)=>{
                    if(data.status==200){
                        examinetipclose();
                        alert("提交审核成功");
                        datas.resultInfo=data.data;
                        reder();
                        $('.paginList').jqPaginator('option', {
                            totalPages: datas.resultInfo.totalPage,
                        });
                    }else alert(data.msg);
                }
            })
        }
        //删除订单信息
        function delOrderInfo(){
            $.ajax({
                url: `/marketOrders/${$("#delOrderId").val()}`,
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf8",
                data: JSON.stringify(datas.conditionInfo),
                success:(data)=>{
                    if(data.status==200){
                        deltipClose();
                        alert("删除成功");
                        datas.resultInfo=data.data;
                        reder();
                        $('.paginList').jqPaginator('option', {
                            totalPages: datas.resultInfo.totalPage,
                        });
                    }else alert(data.msg);
                }
            })
        }
        $(()=>{
            //初始化分页器
            $(".paginList").jqPaginator({
                totalPages: 10,
                visiblePages: 10,
                currentPage: 1,
                activeClass: 'current',
                disableClass: 'disabled',
                prev: '<li class="prev paginItem"><a><span class="pagepre"></span></a></li>',
                next: '<li class="next paginItem"><a><span class="pagenxt"></span></a></li>',
                page: '<li class="page paginItem"><a>{{page}}</a></li>',
                onPageChange: (num) => {
                    getCondition();
                    datas.conditionInfo.pageNum = num;
                    getOrdersPage(window.datas.conditionInfo);
                }
            });
            getDeptJobUser()
        })

    </script>
</head>

<body>
<div class="place"><span>位置：</span>
    <ul class="placeul">
        <li>营销管理</li>
        <li>订购单管理</li>
        <li>基本内容</li>
    </ul>
</div>
<div class="rightinfo">
    <form>
        <ul class="tools">
            <li> 订单编号:
                <input type="text" class="orderid"/>
            </li>
            <li> 客户姓名:
                <input type="text" class="customName"/>
            </li>
            <li> 订购时间:
                <input type="date" class="beOrdertime"/>-<input type="date" class="afOrdertime"/>
            </li>
            <li> 金额:
                <input type="text" class="stinput beOrdermoney"/>-<input type="text" class="stinput afOrdermoney"/>
            </li>
            <li> 审核状态：
                <select class="status">
                    <option value="">请选择</option>
                    <option value="1">未审核</option>
                    <option value="2">审核中</option>
                    <option value="3">审核通过</option>
                    <option value="4">审核未通过</option>
                </select>
            </li>
            <li class="subBut" onclick="search()"><img src="../../../static/images/t06.png"/>查询
            </li>
            <li class="subBut" onclick="window.location.href='orderAdd'"><img src="../../../static/images/t01.png"/>添加
            </li>
        </ul>
        <table class="tablelist">
            <thead>
            <tr>
                <th>序号</th>
                <th>订单编号</th>
                <th>客户姓名</th>
                <th>联系电话</th>
                <th>订购时间</th>
                <th>金额</th>
                <th>操作人</th>
                <th>审核状态</th>
                <th>审核人</th>
                <th>审核时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody class="formbody">

            </tbody>
        </table>
        <div class="pagin">
            <div class="message">共<i class="blue">1256</i>条记录，当前显示第&nbsp;<i class="blue">2&nbsp;</i>页</div>
            <ul class="paginList">
            </ul>
        </div>
    </form>
    <!-- 删除提示框 -->
    <div id="deltip" class="tip">
        <div class="tiptop"><span>提示信息</span><a onclick="deltipClose()"></a></div>
        <div class="tipinfo"><span><img src="../../../static/images/ticon.png"/></span>
            <div class="tipright">
                <p>是否确认删除此条信息？</p>
                <cite>如果是请点击确定按钮 ，否则请点取消。</cite></div>
        </div>
        <div class="tipbtn">
            <input type="hidden" id="delOrderId">
            <input name="" type="button" class="sure" value="确定" onclick="delOrderInfo()"/>
            &nbsp;
            <input name="" type="button" class="cancel" value="取消" onclick="deltipClose()"/>
        </div>
    </div>

    <!-- 审批提示框 -->
    <div id="examinetip" class="tip">
        <div class="tiptop">
            <span>提示信息</span><a onclick="examinetipclose()"></a>
        </div>
        <div class="tipinfo1">

            部门：
            <select class="dfselect dept" onchange="deptChange()">
                <option value="0">请选择</option>
                <option value="1">市场部</option>
                <option value="5">董事会</option>
            </select>
            <p/>
            职位：
            <select class="dfselect job" onchange="jobChange()">
                <option>请选择</option>
            </select>
            <p/>
            姓名：
            <select id="user" class="dfselect">
                <option>请选择</option>
            </select>
            <p/>
        </div>
        <div class="tipbtn">
            <input type="hidden" id="orderid">
            <input name="" type="button" class="sure" value="确定" onclick="submitCheck()"/>
            &nbsp;
            <input name="" type="button" class="cancel" value="取消" onclick="examinetipclose()"/>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
